﻿

$(document).ready(function () {
    //    $(".popup4 .close-popup").click(function (e) {
    //        $('.popup4').hide();
    //        $('#bg-popup-quiz').fadeOut('slow');
    //        reload();
    //    });

    // hide #back-top first
    $("#back-top").hide();

    // fade in #back-top
    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
            $('#back-top').fadeIn();
        } else {
            $('#back-top').fadeOut();
        }
    });

    // scroll body to 0px on click
    $('#back-top a').click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 800);
        return false;
    });
});

function reload() {
    document.location = document.location.href.replace("#", "");
}

// VietID
function openIframe() {
    var overlay = $("<div />").addClass("login-overlay").css("width", $(document).width()).css("height", $(document).height()).appendTo("body");
    var box = $("<div />").addClass("login-box").appendTo("body").center(false);
    var close = $("<a />").addClass("close-button").attr("href", "#").bind("click", function () {
        closeLoginBox();
        return false;
    }).appendTo(box);
    var iframe = $("<iframe />").attr("src", "/LoginMing.aspx").appendTo(box);
}

function openIframeToRegister() {
    var overlay = $("<div />").addClass("login-overlay").css("width", $(document).width()).css("height", $(document).height()).appendTo("body");
    var box = $("<div />").addClass("login-box").appendTo("body").center(false);
    var close = $("<a />").addClass("close-button").attr("href", "#").bind("click", function () {
        closeLoginBox();
        return false;
    }).appendTo(box);
    var iframe = $("<iframe />").attr("src", "/LoginRegister.aspx").appendTo(box);
}

function closeLoginBox(data) {
    var overlay = $(".login-overlay");
    var box = $(".login-box");
    overlay.fadeOut(500, function () { overlay.remove(); });
    box.fadeOut(500, function () { box.remove(); });
    VietID.bindData(data);
}



var VietID = {
    init: function () {
        var me = this;
        $.getScript("http://vietid.net/login/Checksession");
        mingAuthCallBack = function (data1) {
            if (data1 != 'null') {
                //co session tren ming se tu dong login
                var rels = eval('(' + data1 + ')');

                strQuery = "username=" + rels.username + "&email=" + rels.email + "&full_name=" + rels.full_name + "&id=" + rels.id + "&checksum=" + rels.checksum + "&avatar=" + rels.avatar;

                $.getScript("/dologin.aspx?" + strQuery);
                //                a = function (data) {
                //                    if (data != null) {

                //                        if (data.UrlRound == '#') {
                //                            $("#uName").html(data.UDisplay);
                //                            $("#uName").attr('href', data.ULink);
                //                        }
                //                        else {
                //                            $("#uName").html(data.UName);
                //                            $("#uName").attr('href', data.UrlRound);
                //                        }
                //                        $('#hdLogin').val(data.UName);
                //                        $("#login").show();
                //                        $("#logout").hide();

                //                        me.reloadIFrame();

                //                    }
                //                }
            }
        };
    },
    bindData: function (data) {
        var me = this;
        if (data != null) {
            if (data.UrlRound == '#' && data.ULink.length > 0) {
                $("#uName").html(data.UName);
                $("#uName").attr('href', data.ULink);
            }
            else if (data.UrlRound.length > 1) {
                $("#uName").html(data.UName);
                $("#uName").attr('href', data.UrlRound);
            }
            $("#uLink").html(data.UDisplay);
            $("#uLink").attr('href', data.ULink);
            $('#hdLogin').val(data.UName);
            if (data.UrlRound != '' && data.UrlRound != null && data.UrlRound != '#') $(".uBreak").show();
            else if (data.UDisplay != '' && data.UDisplay != null) $(".uBreak").show();
            if (data.ULink.length <= 0) {
                $("#uLink").hide().next().hide();
            }
            $("#login").show();
            $("#logout").hide();

            me.reloadIFrame();

        }
    },
    reloadIFrame: function () {

        if ($('#mingid_comment_iframe').length > 0) {
            $('#mingid_comment_iframe').attr('src', $('#mingid_comment_iframe').attr('src'));
        }

        if ($('.mingid_comment_iframe').length > 0) {

            $('.mingid_comment_iframe').each(function () {
                $(this).attr('src', $(this).attr('src'));
            });
        }

        return false;
    },
    logout: function () {
        var me = this;
        var i = new Image();
        i.src = "http://vietid.net/login/LogoutService";

        $('.cm .btnreply').removeClass('active');
        $('.cm .boxReply').remove();

        $.getScript('/dologin.aspx?type=1&c=' + Math.random());
        abc = function (data) {
            var b = 1;
            while (data == null) {
                b++;
                if (b > 200) {
                    break;
                }
            }
        };

        $('#hdLogin').val('');

        $('#logout').show();
        $('#login').hide();

        setTimeout(function () {
            me.reloadIFrame();
        }, 1000);
    }
};

$(document).ready(function () {
    VietID.init();
});

/***************************************
= SendQA
-------------------------------------- */
function sendQA() {
    var $userName = $('#qa_name');
    var $userEmail = $('#qa_email');
    var $userContent = $('#qa_content');
    var param = {};
    if ($userName.val() == '') {
        alert('Họ tên không được để trống!');
        $userName.focus();
        return;
    }
    var regex = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if ($userEmail.val() == '' || !regex.test($userEmail.val())) {
        alert('Email không hợp lệ!');
        $userEmail.focus();
        return;
    }
    if ($userContent.val() == '') {
        alert('Nội dung không được để trống!');
        $userContent.focus();
        return;
    }

    param['command'] = "sendqa";
    param['name'] = $userName.val();
    param['email'] = $userEmail.val();
    param['content'] = $userContent.val();
    $.ajax({
        type: "POSt",
        url: "/Handlers/AuthenticationHandler.ashx",
        data: param,
        dataType: 'json',
        success: function (data) {
            if (data.Type === 1) { openIframe(); return; }
            if (data.Success === true) {
                $('#qa_name').val("");
                $('#qa_email').val("");
                $('#qa_content').val("");
            }
            alert(data.Message);
        }
    });

}

var photoAlbum = {
    index: 1,
    count: 0,
    cols: 3,
    id: '.list_girl',
    size: 6,
    round: 1,
    containers: [],
    flag: true,
    init: function () {
        var me = this;

        if ($(me.id).length < 1) {
            return;
        }
        me.containers[0] = $('<li class="left fl w300 mr20"></li>').appendTo($(me.id));
        me.containers[1] = $('<li class="center fl  w300 mr20"></li>').appendTo($(me.id));
        me.containers[2] = $('<li class="right fl  w300 last"></li>').appendTo($(me.id));
        me.preload(1);
        $(window).scroll(function () {
            if ($(window).scrollTop() + $(window).height() > $(document).height() - 200) {
                me.preload(me.index);
            }
        });
    },
    preload: function (pageIndex) {
        var me = this;
        if (me.flag == false) { return; }

        me.flag = false;

        $.ajax({
            url: '/Handlers/EventHandler.ashx',
            data: ({ 'command': 'getphotoalbum', 'pi': pageIndex, 'round': me.round, 'ps': me.size }),
            dataType: 'json',
            success: function (data) {
                if (data) {
                    me.flag = true;
                    me.index++;
                    me.count += me.size;
                    me.bindData($(me.id), data);
                }
            }
        });
    },
    bindData: function (obj, items) {
        if (!obj || !items) { return; }

        var me = this;
        // 0 = title, 1 = detail link, 2 = title, 3 = img src, 4 = encode title, 5 = desc, 6 = avatar, 7 = name, 8 = vote
        var temp = "<div class=\"list-photo-all fl mt20\">";
        temp += "<a title=\"{1}\" desc=\"{4}\" etitle=\"{3}\" avatar=\"{5}\" name=\"{6}\" vote=\"{7}\" url=\"{0}\" uid=\"{10}\" href=\"javascript:void(0)\">";
        temp += "<img alt=\"\" title=\"\" src=\"{2}\" {8} {9} />";
        temp += "</a>";
        temp += "</div>";
        for (i in items) {
            var ele = $(String.format(temp, items[i].Url, items[i].EncodeName
            , items[i].ThumbUrl, items[i].EncodedBase64Title, items[i].Photo_Description
            , items[i].Avatar45x45, items[i].EncodeName, items[i].Vote
            , items[i].Width > 0 ? String.format("w =\"{0}\"", items[i].Width) : ''
            , items[i].Height > 0 ? String.format("h =\"{0}\"", items[i].Height) : '', items[i].Member_ID));
            $(me.containers[(me.count + i) % me.cols]).append(ele);
        }
        photoDetail.lockVote = $('#hfLockVote').val();
        photoDetail.displayVote = $('#hfDisplayVote').val();
        photoDetail.init();
    }
};
// timeline trang chu
function timelineHomepage(now) {
    var item = $('#timeline-point');
    item.show();
    var timeNow = new Date(now);
    var dateRound0 = new Date('05 06 2013');
    var dateRound1 = new Date('05 10 2013');
    var dateRound2 = new Date('05 21 2013');
    var dateRound3 = new Date('06 03 2013');
    var diff0 = new Date(dateRound1 - dateRound0);
    var days0 = diff0 / 1000 / 60 / 60 / 24;
    var diff1 = new Date(dateRound2 - dateRound1);
    var days1 = diff1 / 1000 / 60 / 60 / 24;
    var diff2 = new Date(dateRound3 - dateRound2);
    var days2 = diff2 / 1000 / 60 / 60 / 24;
    if (timeNow >= dateRound0 && timeNow <= dateRound1) {

        var temp = 98 / days0;
        var diffNow = new Date(timeNow - dateRound0);
        var daysNow = diffNow / 1000 / 60 / 60 / 24;
        $(item).css('left', 0 + (temp * daysNow) + "px");
    }
    else if (timeNow > dateRound1 && timeNow <= dateRound2) {

        var temp = 196 / days1;
        var diffNow = new Date(timeNow - dateRound1);
        var daysNow = diffNow / 1000 / 60 / 60 / 24;
        $(item).css('left', 100 + (temp * daysNow) + "px");
    }
    else if (timeNow > dateRound2 && timeNow <= dateRound3) {
        var temp = 174 / days2;
        var diffNow = new Date(timeNow - dateRound2);
        var daysNow = diffNow / 1000 / 60 / 60 / 24;
        $(item).css('left', 296 + (temp * daysNow) + "px");
    } else if (timeNow > dateRound3) $(item).css('left', "470px");
    else if (timeNow < dateRound1) $(item).hide(); //$(item).css('left', 0);
}


/***************************************
= Vote
-------------------------------------- */
var Vote = {
    handler: '/Handlers/AuthenticationHandler.ashx',
    voteMember: function (id) {
        var me = this;
        var param = {};
        param['command'] = 'vote';
        param['id'] = id;
        $.ajax({
            type: "POST",
            url: me.handler,
            data: param,
            dataType: 'json',
            success: function (data) {
                if (data.Type == 1) { openIframe(); return; }
                if (data.Success === true) {
                    me.upVote(id);
                }
                else {
                    alert(data.Message);
                };
            }
        });
        //me.upVote(id);
    },
    upVote: function (id) {
        // trang ca nhan
        var $item = $('#detail-vote-btn');
        var count = $item.attr('vote');
        count = parseInt(count) + 1;
        $('#detail-vote').html(count);
        $item.addClass('active').removeAttr('onclick').removeAttr('style').unbind('hover').text('VOTED');

        //lightbox
        var $lb = $('#popup-detail-bottom-vote');
        count = $lb.attr('vote');
        count = parseInt(count) + 1;
        $lb.html(count + " VOTED");
        $lb.addClass('active').removeAttr('onclick').removeAttr('style').unbind('hover').unbind('click');

        // list
        var $lst = $('.vote-bt-' + id);
        if ($lst != null || $lst != undefined) {
            count = $lst.attr('vote');
            count = parseInt(count) + 1;
            $lst.html(count + " VOTED");
            $lst.addClass('active').removeAttr('onclick').removeAttr('style').unbind('hover');
        }

        // list photo
        $('.list_girl').find('a[uid="' + id + '"]').each(function () {
            count = $(this).attr('vote');
            count = parseInt(count) + 1;
            $(this).attr('vote', count);
        });
    }
};

/***************************************
= Upload
-------------------------------------- */
FormUploader = {
    domainStorage: 'http://channel.vcmedia.vn',
    handler: '/Handlers/AuthenticationHandler.ashx',
    uploadAvatarCommand: 'uploadavatar',
    multiUploadCommand: 'multiuploadimages',
    cropAvatarCommand: 'cropavatar',
    loadPhotosCommand: 'loadphotos',
    btnUserAvatarClass: '.btnUserAvatar',
    btnAlbumAvatarClass: '.btnAlbumAvatar',
    idUserAvatar: '#userAvatar',
    idAlbumAvatar: '#albumAvatar',
    defaultAvatar: '/images/choose-images.png',
    defaultAlbum: '/images/choose-images2.png',
    currentAvatar: null,
    reUpload: false,
    jcropAPI: null,
    data: null,
    container: null,
    count: 0,
    maxItems: 20,
    widthImage: 600,
    defaultDesc: 'Điền chú thích ảnh',
    init: function () {
        var me = this;
        $(me.btnUserAvatarClass).ajaxUploadPrompt({
            url: me.handler,
            data: { 'command': me.uploadAvatarCommand },
            onprogress: function (e) {
                if (e.lengthComputable) {
                    var $src = $(me.idUserAvatar).attr('src');
                    if ($src == me.defaultAvatar) $(me.idUserAvatar).attr('src', '').addClass('upload-loading');
                    else {
                        me.currentAvatar = $src;
                        $(me.idUserAvatar).attr('src', '').addClass('upload-loading');
                        me.reUpload = true;
                    }
                }
            },
            success: function (data, status, xhr) {
                data = eval('(' + data + ')');
                var $src = $(me.idUserAvatar).attr('src');
                if (data.Type == 1) {
                    if ($src == '' && me.reUpload === false) $(me.idUserAvatar).attr('src', me.defaultAvatar);
                    else if (me.reUpload === true) {
                        $(me.idUserAvatar).attr('src', me.currentAvatar);
                        me.reUpload = false;
                        me.currentAvatar = null;
                    }
                    openIframe(); return;
                }
                if (data.Success === true) me.bindCrop(data.Data, 220, 220, 1, me.idUserAvatar);
                else {
                    if ($src == '' && me.reUpload === false) $(me.idUserAvatar).attr('src', me.defaultAvatar);
                    else if (me.reUpload === true) {
                        $(me.idUserAvatar).attr('src', me.currentAvatar);
                        me.reUpload = false;
                        me.currentAvatar = null;
                    }
                    alert(data.Message);
                };
            }
        });
        $(me.btnAlbumAvatarClass).ajaxUploadPrompt({
            url: me.handler,
            data: { 'command': me.uploadAvatarCommand },
            onprogress: function (e) {
                if (e.lengthComputable) {
                    var $src = $(me.idAlbumAvatar).attr('src');
                    if ($src == me.defaultAlbum) $(me.idAlbumAvatar).attr('src', '').addClass('upload-loading');
                    else {
                        me.currentAvatar = $src;
                        $(me.idAlbumAvatar).attr('src', '').addClass('upload-loading');
                        me.reUpload = true;
                    }
                }
            },
            success: function (data, status, xhr) {
                data = eval('(' + data + ')');
                var $src = $(me.idAlbumAvatar).attr('src');
                if (data.Type == 1) {
                    if ($src == '' && me.reUpload === false) $(me.idAlbumAvatar).attr('src', me.defaultAlbum);
                    else if (me.reUpload === true) {
                        $(me.idAlbumAvatar).attr('src', me.currentAvatar);
                        me.reUpload = false;
                        me.currentAvatar = null;
                    }
                    openIframe(); return;
                }
                if (data.Success === true) {
                    me.bindCrop(data.Data, 220, 330, 220 / 330, me.idAlbumAvatar);
                }
                else {
                    if ($src == '' && me.reUpload === false) $(me.idAlbumAvatar).attr('src', me.defaultAlbum);
                    else if (me.reUpload === true) {
                        $(me.idAlbumAvatar).attr('src', me.currentAvatar);
                        me.reUpload = false;
                        me.currentAvatar = null;
                    }
                    alert(data.Message);
                };
            }
        });
        me.bindButtonUpload();
        me.loadPhoto();
        $('#submitForm').bind('click', function () { me.submitForm(); });
    },
    bindCrop: function (url, width, height, ratio, control) {
        var me = this;
        $(".popup4").show().center(true);
        $('#bg-popup').css({ 'opacity': '0.8' });
        $('#bg-popup').fadeIn('slow');
        var wHeight = document.documentElement.clientHeight;
        $('#bg-popup-quiz').css({ 'height': wHeight }); // only need force for IE6
        var $img = $('<img src="" id="imgCrop_' + height + '" alt="" />');
        $('.photo-crop').append($img);
        $(".popup4 img[id*=imgCrop]").attr('src', me.domainStorage + url);
        $('img[id*=imgCrop]').Jcrop({
            onChange: storeCoords,
            onSelect: storeCoords,
            minSize: [width, height],
            setSelect: [100, 100, 50, 50],
            aspectRatio: ratio
        }, function () { me.jcropAPI = this; });
        $('#btnCrop').bind('click', function () { me.crop(control); });
        $('#btnCropCancel').bind('click', function () { me.hideCrop(control); });
    },
    hideCrop: function (control) {
        var me = this;
        $(".popup4").hide();
        $('#btnCrop').unbind('click');
        $('#btnCropCancel').unbind('click');
        $('#bg-popup').fadeOut('slow');
        me.jcropAPI.destroy();
        $('.photo-crop').find('img[id*=imgCrop]').remove();
        var $src = $(control).attr('src');
        if ($src == '' && me.reUpload === false) $(control).attr('src', me.defaultAvatar);
        else if ($src == '' && me.reUpload === true) {
            $(control).attr('src', me.currentAvatar);
            me.reUpload = false;
            me.currentAvatar = null;
        }
        else {
            me.reUpload = false;
            me.currentAvatar = null;
        }
    },
    crop: function (control) {
        var me = this;
        var img = $('img[id*=imgCrop]').attr('src');
        var x = $('#X').val();
        var y = $('#Y').val();
        var w = $('#W').val();
        var h = $('#H').val();

        $.ajax({
            url: me.handler,
            data: { 'command': me.cropAvatarCommand, 'image': img, 'x': x, 'y': y, 'w': w, 'h': h },
            dataType: 'json',
            success: function (data) {
                if (data.Type == 1) { openIframe(); return; }
                if (data.Success === true) {
                    me.bindAvatar(control, data.Data);
                    me.hideCrop();
                };
            }
        });
    },
    bindAvatar: function (control, url) {
        var me = this;
        $(control).attr('src', me.domainStorage + url);
    },
    bindButtonUpload: function () {
        var me = this;

        if (me.count > 0) {
            $('.form-upload-btn-default').remove();
        }
        else {
            $('.form-upload-btn-default').remove();
            $('#form-upload-note').after('<a class="form-upload-btn form-upload-btn-default" href="javascript:void(0);">Chọn ảnh tải lên</a>');
        }
        if (me.count >= me.maxItems) {
            $('.form-upload-add').remove();
        }
        else {
            $('.form-upload-add').remove();
            if (me.count > 0) $('.upload-button').append('<a class="form-upload-btn form-upload-add" href="javascript:void(0);">Thêm ảnh</a>');
            $('.form-upload-btn').ajaxUploadPrompt({
                url: me.handler,
                data: { 'command': me.multiUploadCommand, 'count': me.count },
                onprogress: function (e) {
                    if (e.lengthComputable) {
                        $('.upload-uploading').show();
                    }
                },
                success: function (data, status, xhr) {
                    $('.upload-uploading').hide();
                    data = eval('(' + data + ')');
                    if (data.Type == 1) { openIframe(); return; }
                    me.data = data;
                    me.bindData();
                    me.bindHover();
                    me.bindTextArea();
                    me.bindButtonUpload();
                }
            });
        }
    },
    bindData: function () {
        var me = this;
        me.containers = $('.form-upload-list');

        if (me.data.Success === false)
            me.bindNotify("- " + me.data.Message);

        for (var i = 0; i < me.data.Total; i++) {
            if (me.data.Data[i].Success === true) {
                var item = me.bindItem(me.data.Data[i].Data, me.data.Data[i].ExData, me.data.Data[i].ExSize);
                $(me.containers).append(item);
                me.count++;
            }
            else {
                me.bindNotify("- " + me.data.Data[i].Message);
            }
        }

        var checkNotify = $('.upload-notify').html();
        if (checkNotify != null && checkNotify.length > 0) {
            $('.upload-notify').show();
            setTimeout(function () {
                $('.upload-notify').fadeOut('fast', function () { $(this).html(""); });
            }, 8000);
        }
    },
    bindItem: function (image, desc, size) {
        var me = this;
        // 0 = thumb image, 1 = original image, 2 = size image
        var template = '<div class="form-upload-item">';
        template += '<i style="display: none;" class="form-upload-delete" onclick="FormUploader.deleteItem(this);"></i><i style="display: none;" class="form-upload-delete-caption"></i>';
        template += '<img alt="" src="{0}" />';
        template += (desc == '' || desc == null) ? '<textarea class="upload-item-desc" maxlength="500">Điền chú thích ảnh</textarea>' : '<textarea class="upload-item-desc" maxlength="500">' + desc + '</textarea>';
        template += '<input class="upload-item-hidden" type="hidden" value="{1}" />';
        template += '<input class="upload-size-hidden" type="hidden" value="{2}" />';
        template += '</div>';
        return String.format(template, me.thumb_w(me.widthImage, image), image, size);
    },
    bindHover: function () {
        $('.form-upload-item').hover(function () {
            $(this).find('.form-upload-delete').fadeIn('fast');
        }, function () {
            $(this).find('.form-upload-delete').hide();
        });
        $('.form-upload-delete').hover(function () {
            $(this).parent().find('.form-upload-delete-caption').show();
        }, function () {
            $(this).parent().find('.form-upload-delete-caption').hide();
        });
    },
    bindTextArea: function () {
        var me = this;
        $('.upload-item-desc').bind('focus', function () {
            if ($(this).val() == me.defaultDesc) {
                $(this).val("");
                $(this).addClass('upload-item-desc-focus');
            }
        });
        $('.upload-item-desc').bind('blur', function () {
            if ($(this).val() == "") {
                $(this).val(me.defaultDesc);
                $(this).removeClass('upload-item-desc-focus');
            }
        });
    },
    bindNotify: function (mess) {
        $('.upload-notify').append(String.format('<p>{0}</p>', mess));
    },
    deleteItem: function (control) {
        var me = this;
        var item = $(control).parent();
        item.fadeOut('fast', function () { $(this).remove(); });
        me.count--;
        me.bindButtonUpload();
    },
    submitForm: function () {
        var me = this;
        var $txtFullName = $('#txtFullName');
        var regex = /[^a-zA-ZàáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ ]+/;
        if ($txtFullName.val() == '' || regex.test($txtFullName.val())) {
            alert('Tên đầy đủ không hợp lệ!');
            $txtFullName.focus();
            return false;
        }

        //                var $txtEmail = $('#txtEmail');
        //               var regex = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        //                if ($txtEmail.val() == '' || !regex.test($txtEmail.val())) {
        //                    alert('Tên đầy đủ không được để trống!');
        //                    $txtEmail.focus();
        //                    return false;
        //                }

        var $txtAddress = $('#txtAddress');
        if ($txtAddress.val() == '') {
            alert('Địa chỉ không được để trống!');
            $txtAddress.focus();
            return false;
        }

        var $txtPhone = $('#txtPhone');
        if ($txtPhone.val() == '') {
            alert('Số điện thoại không được để trống!');
            $txtPhone.focus();
            return false;
        }

        var $userAvatar = $('#userAvatar').attr('src');
        if ($userAvatar == '' || $userAvatar == me.defaultAvatar) {
            alert('Avatar không được để trống!');
            return false;
        }

        var $albumAvatar = $('#albumAvatar').attr('src');
        //        if ($albumAvatar == '' || $albumAvatar == me.defaultAvatar) {
        //            alert('Avatar bài thi không được để trống!');
        //            return false;
        //        }

        var $txtTitle = $('#txtTitle');
        //        if ($txtTitle.val() == '') {
        //            alert('Tên bài dự thi không được để trống!');
        //            $txtTitle.focus();
        //            return false;
        //        }


        var ddlDay = $('#ddlDay').val();
        var ddlMonth = $('#ddlMonth').val();
        var ddlYear = $('#ddlYear').val();
        var ddlGender = $('#ddlGender').val();
        var ddlCity = $('#ddlCity').val();
        var txtAbout = $('#txtAbout').val();
        var txtDesc = $('#txtDesc').val();
        $userAvatar = $userAvatar.replace(me.domainStorage, '');
        $albumAvatar = $albumAvatar == me.defaultAlbum ? '' : $albumAvatar.replace(me.domainStorage, '');

        var images = [];
        $('.form-upload-list').find('.form-upload-item').each(function () {
            var img = $(this).find('.upload-item-hidden').val();
            var size = $(this).find('.upload-size-hidden').val();
            var desc = $(this).find('.upload-item-desc').val();
            if (desc == me.defaultDesc) desc = "";
            images.push({ Image: img, Desc: desc, Size: size });
        });

        if (images.length > 0) {
            if ($albumAvatar == '' || $albumAvatar == me.defaultAlbum) {
                        alert('Avatar bài thi không được để trống!');
                        return false;
                    }
                    if ($txtTitle.val() == '') {
                        alert('Tên bài dự thi không được để trống!');
                        $txtTitle.focus();
                        return false;
                    }            
        }
        
        var $Images = JSON.stringify(images);

        var param = { command: "uploadconfirm", fullname: $txtFullName.val(), city: ddlCity //, email: $txtEmail.val()
                , address: $txtAddress.val(), phone: $txtPhone.val(), useravatar: $userAvatar, albumavatar: $albumAvatar
                , title: $txtTitle.val(), day: ddlDay, month: ddlMonth, year: ddlYear, gender: ddlGender, about: txtAbout
                , desc: txtDesc, images: $Images
        };
        $.ajax({
            url: me.handler,
            data: param,
            type: "POST",
            success: function (data) {
                data = eval('(' + data + ')');
                if (data.Success === true) {
                    //alert(data.Message);
                    alert(data.Message);
                    window.location.href = data.ExData;
                }
                else {
                    if (data.Type == 1) openIframe();
                    else alert(data.Message);
                }
            }
        });
    },
    loadPhoto: function () {
        var me = this;
        $.ajax({
            url: me.handler,
            data: { 'command': me.loadPhotosCommand },
            dataType: 'json',
            success: function (data) {
                if (data.Type == 1) { openIframe(); return; }
                if (data.Success === true) {
                    me.data = data;
                    me.bindData();
                    me.bindHover();
                    me.bindTextArea();
                    me.bindButtonUpload();
                };
            }
        });
    },
    thumb_w: function (w, filename) {
        var me = this;
        return me.domainStorage + "/thumb_w/" + w + "" + filename;
    }
};

function storeCoords(c) {
    $('.popup4 input[id$=X]').val(c.x);
    $('.popup4 input[id$=Y]').val(c.y);
    $('.popup4 input[id$=W]').val(c.w);
    $('.popup4 input[id$=H]').val(c.h);
};
